package com.kitty.game.mijing;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.model.product.ActivityType;
import com.kitty.game.activity.service.time.FightActivityHandler;
import com.kitty.game.boss.config.BossSet;
import com.kitty.game.boss.model.BossFightParam;
import com.kitty.game.boss.model.BossParam;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.config.NPC;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.fight.service.FightService;
import com.kitty.game.mijing.model.MijingRank;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.model.Team;
import com.kitty.game.utils.Const;
import com.kitty.listener.event.FightEndEvent;
import com.kitty.mina.message.MessagePusher;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

@Component
public class MinjingHander extends FightActivityHandler {
   @Autowired
   ChatService chatService;
   @Autowired
   Dao dao;
   @Autowired
   FightService fightService;
   private static final int ACCEPT_MIN_LEVEL = 60;
   private static final List<String> SUPER_BOSS_LIST = Arrays.asList("离火", "乾金", "艮木", "坎水", "坤土");
   private static final String IN_FIGHT = "攻城怪物来袭，尔等凡人，速速受死！[离开]";
   private static final String TEAM_LEVEL_NOT_ENOUGH = "攻城怪物来袭，尔等凡人，也敢来挑衅（队伍中#Y{0}#n的等级不足{1}级，能力不足以除害）[离开]";

   protected String getNpcContent(Role role, NPC bossNpc) {
      return null;
   }

   public String getNpcContentNotFight(Role role, NPC bossNpc) {
      if (bossNpc.isInFight()) {
         return "攻城怪物来袭，尔等凡人，速速受死！[离开]";
      } else {
         int teamCount = this.teamService.getTeamCount(role);
         if (teamCount > 1) {
            return "";
         } else {
            Team team = this.teamService.getTeam(role.getRoleId());
            String names = this.teamService.checkMember(team, (memberRole) -> {
               return memberRole.getLevel() < 60;
            });
            return names != null ? MessageFormat.format("攻城怪物来袭，尔等凡人，也敢来挑衅（队伍中#Y{0}#n的等级不足{1}级，能力不足以除害）[离开]", names, 60) : null;
         }
      }
   }

   public void doStartFight(Role role, NPC bossNpc) {
      int teamCount = this.teamService.getTeamCount(role);
      if (teamCount > 1) {
         MessagePusher.pushMessage((Role)role, new RespNotifyMiscEx("秘境请单人挑战，无法组队进行！"));
      } else {
         Random random = new Random();
         int randomI = random.nextInt(SUPER_BOSS_LIST.size());
         String npcname = (String)SUPER_BOSS_LIST.get(randomI);
         bossNpc = new NPC();
         bossNpc.setName(npcname);
         bossNpc.setBossSetName(npcname);
         bossNpc.setId(88893);
         List<BossParam> bossParamList = this.newBossParamList(role, bossNpc);
         BossFightParam bossFightParam = new BossFightParam(bossParamList, this.getFightType(role));
         bossFightParam.setNpcId(bossNpc.getId());
         this.bossService.startFightToBoss(role, bossFightParam);
      }
   }

   @Override
   protected int getFightType(Role role) {
      return Const.mijing;
   }

   protected NPC getBossNpc(int npcId) {
      return SpringUtils.getMapService().getNpc(npcId);
   }

   protected void clearNpcAfterWin(Role role, NPC bossNpc) {
   }

   private List<BossParam> newBossParamList(Role role, NPC bossNpc) {
      List<BossParam> bossParamList = new ArrayList();
      int count = 10;
      BossSet bossSet = this.bossService.getBossSet(bossNpc.getBossSetName());
      bossParamList.add(new BossParam(bossSet, bossNpc.getName()));

      for(int i = 1; i < count; ++i) {
         Random random = new Random();
         int randomI = random.nextInt(SUPER_BOSS_LIST.size());
         String npcname = (String)SUPER_BOSS_LIST.get(randomI);
         bossSet = this.bossService.getBossSet(npcname);
         bossParamList.add(new BossParam(bossSet, npcname));
      }

      return bossParamList;
   }

   public int getBossCount(Role role) {
      int allCount = 10;
      int teamCount = this.teamService.getTeamCount(role);
//      int count = false;
      byte minCount;
      int count;
      if (teamCount <= 3) {
         minCount = 6;
         count = ThreadLocalRandom.current().nextInt(allCount - minCount + 1) + minCount;
      } else if (teamCount == 4) {
         minCount = 8;
         count = ThreadLocalRandom.current().nextInt(allCount - minCount + 1) + minCount;
      } else {
         count = allCount;
      }

      return count;
   }
   private byte getRoleRemainCount(Role role) {
      return (byte) SpringUtils.getActivityService().getRemainCount(role, ActivityType.mijing);
   }
   public void giveReward(Role role, FightEndEvent fightEndEvent, NPC bossNpc) {
      int count = getRoleRemainCount(role);
      if (count > 0) {/*次数在npc类 这个0只是取数*/
//         if (!teamService.isInTeam(role)) {
//            MessagePusher.pushMessage(role, new RespNotifyMiscEx("离队无法获得奖励"));
//            return;
//         }
         // 这里改成公共获取配置 经验，道行，武学 奖励。 不再单独写
         fightService.sendBossBasicsReward(role,fightEndEvent);
         SpringUtils.getActivityService().addFinishCount(role, ActivityType.mijing, 1);
         fightService.sendBossExtReward(role,fightEndEvent);


      } else{
         MessagePusher.pushMessage(role, new RespNotifyMiscEx("今日挑战次数以达到上限！不会给奖励"));
         return;
      }
      //SpringUtils.getActivityService().addFinishCount(role, ActivityType.mijing, 1);
      int mijingcishu = role.getMijingcount();
      role.setMijingcount(mijingcishu + 1);
      this.mijinginsert(role);
   }

   public void mijinginsert(Role role) {
      MijingRank mijingRank = new MijingRank();
      mijingRank.setId(role.getRoleId());
      mijingRank.setRolename(role.getName());
      mijingRank.setWushang(role.getMaxPhyPower());
      mijingRank.setFashang(role.getMaxMagPower());
      mijingRank.setFangyu(role.getMaxDefense());
      mijingRank.setSudu(role.getMaxSpeed());
      mijingRank.setGuanqia(role.getMijingcount());
      mijingRank.setParty(role.getPartyName());
      mijingRank.setGid(role.getGid());
      mijingRank.setGuanqia(role.getMijingcount());
      this.dao.insertOrUpdate(mijingRank);
   }

   protected void clearNpcTimeOut(NPC npc) {
   }
}
